### Project 13 Tilt Switch ![](media/image-20251121172558920.png) **1. Introduction** Tilt switch controlling the LED ON and OFF. **2. Hardware Required** 1. Ball switch *1 2. Control board *1 3. Led *1 4. 220Ω resistor *1 5. 10KΩ resistor*1 6. USB cable *1 7. Breadboard jumper wire *5 **3. Connection for REV4** Connect the ball tilt switch, LED and resistors to control board. Connect the LED to digital pin 8, ball switch to analog pin 5. ![](media/image-20251121172833445.png) **4. Connection for 2560 R3** ![](media/image-20251121172905696.png) **5. Experiment Principle** When one end of the switch is below horizontal position, the switch is on. The voltage of the analog port is about 5V (1023 in binary). The LED will be on. When the other end of the switch is below horizontal position, the switch is off. The voltage of the analog port is about 0V (0 in binary). The LED will be off. In the program, we determine whether the switch is on or off according to the voltage value of the analog port, whether it's above 2.5V (512 in binary) or not. **6. Sample Code** ```c void setup() { pinMode(8,OUTPUT);// set digital pin 8 as “output” } void loop() { int i;// define variable i while(1) { i=analogRead(5);// read the voltage value of analog pin 5 if(i>512)// if larger that 512(2.5V) { digitalWrite(8,LOW);// turn on LED } else// otherwise { digitalWrite(8,HIGH);// turn off LED } } } ``` **7. Result** Hold the breadboard with your hand. Tilt it to a certain extent, the LED will be on. If there is no tilt, the LED will be off.The principle of this experiment can be applied to relay control. Experiment is completed. Thank you! ![](media/image-20251121173238368.png)